package com.alphathink.qrcodecard.Activity;

import java.io.File;

import com.alphathink.BitMapUtil.BitmapDiskCache;
import com.alphathink.qrcodecard.model.QRCodeCardModel;
import com.alphathink.qrcodecard.model.QRCodeCardNetModel;
import com.alphathink.xpg.qrcodecard.R;
import com.google.gson.Gson;
import com.google.zxing.WriterException;
import com.zxing.encoding.EncodingHandler;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 对用户输入的信息进行获取并进行生成二维码
 * 
 * @category 可进行分享
 * 
 * @author xpg
 *
 */

public class QRCode extends Activity {
	private static final String TAG = "QRCode生成的界面";
	private ImageView mImagetViewQr, mImageViewWeiX, mImageViewQQ;

	private Button mBuutonSave;

	private String mStr;
	
	//
	private static final String APP_ID ="";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_qrcode);

		mStr = (String) getIntent().getSerializableExtra("data");
		initView();

	}

	private void initView() {
		// 要显示生成的二维码的图片区域
		mImagetViewQr = (ImageView) findViewById(R.id.qrcode_IV_qrcodeGen);
		mImageViewQQ = (ImageView) findViewById(R.id.qrcode_QqshareIV);
		mImageViewWeiX = (ImageView) findViewById(R.id.qrcode_WeiChatIV);

		mBuutonSave = (Button) findViewById(R.id.qrcode_SaveBtn);

		QRCodeCardNetModel cardNetModel = new QRCodeCardNetModel();
		Gson gson = new Gson();
		cardNetModel = gson.fromJson(mStr, QRCodeCardNetModel.class);
		System.out.println("gson");

		// 顶一个bitmap的对象，用于生成二维码
		final Bitmap mQRGenBitmap;
		final QRCodeCardModel cardModel = cardNetModel.getData().get(0);
		System.out.println(TAG + "----->" + cardModel.getUserName());
		try {
			mQRGenBitmap = EncodingHandler.createQRCode(mStr, 350);
			mImagetViewQr.setImageBitmap(mQRGenBitmap);

			// 保存的界面，保存到本地
			mBuutonSave.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Log.d(TAG, "点击了保存.");
					BitmapDiskCache.getInstance().cacheBitmap(mQRGenBitmap,
							cardModel.getUserName());
					Log.d(TAG, "保存成功");
					Toast.makeText(
							getApplication(),
							"图片保存到了:" + Environment.getExternalStorageState()
									+ File.separator + "QRCoderCard"
									+ File.separator + "pic",
							Toast.LENGTH_SHORT).show();
				}
			});
		} catch (WriterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_qrcode, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();

		// noinspection SimplifiableIfStatement
		if (id == R.id.action_settings) {
			return true;
		}

		return super.onOptionsItemSelected(item);
	}
}
